<template>
	<el-main>
		<el-card class="task task-item" shadow="hover" header="试题信息">
			<h2>{{ question.questionName }}</h2>
			<ul>
				<li>
					<h4>描述</h4>
					<div
						v-html="question.questionDescribe"
						style="overflow: auto !important"
					></div>
				</li>
			</ul>
		</el-card>

		<el-row :gutter="15" style="margin-top: 10px">
			<el-col
				:xl="6"
				:lg="6"
				:md="8"
				:sm="12"
				:xs="24"
				v-for="item in list"
				:key="item.id"
			>
				<el-card class="task task-item" shadow="hover">
					<h2>{{ item.langText }}</h2>
					<ul>
						<li>
							<h4>示例文件名称</h4>
							<p>{{ item.examplesFileName }}</p>
						</li>
						<li>
							<h4>执行文件名</h4>
							<p>{{ item.execFileName }}</p>
						</li>
					</ul>
					<div class="bottom">
						<div class="state">
							<el-tag v-if="item.state == '1'" size="small"
								>准备就绪</el-tag
							>
						</div>
						<div class="handler">
							<el-dropdown trigger="click">
								<el-button
									type="primary"
									icon="el-icon-more"
									circle
									plain
								></el-button>
								<template #dropdown>
									<el-dropdown-menu>
										<el-dropdown-item @click="edit(item)"
											>编辑</el-dropdown-item
										>
										<el-dropdown-item
											@click="del(item)"
											divided
											>删除</el-dropdown-item
										>
									</el-dropdown-menu>
								</template>
							</el-dropdown>
						</div>
					</div>
				</el-card>
			</el-col>
			<el-col :xl="6" :lg="6" :md="8" :sm="12" :xs="24">
				<el-card class="task task-add" shadow="never" @click="add">
					<el-icon><el-icon-plus /></el-icon>
					<p>添加试题语言</p>
				</el-card>
			</el-col>
		</el-row>
	</el-main>

	<save-dialog
		v-if="dialog.save"
		ref="saveDialog"
		@success="handleSuccess"
		@closed="dialog.save = false"
	></save-dialog>
</template>

<script>
import saveDialog from "./components/languageSave";

export default {
	name: "language",
	components: {
		saveDialog,
	},
	provide() {
		return {
			list: this.list,
		};
	},
	data() {
		return {
			dialog: {
				save: false,
				logsVisible: false,
			},
			questionId: "",
			question: {},
			list: [
				{
					id: "1",
					title: "清理服务器缓存",
					handler: "cleanUpCacheHandler",
					cron: "59 59 23 * * ? *",
					state: "1",
				},
				{
					id: "2",
					title: "自动审核",
					handler: "automaticAuditHandler",
					cron: "0 0 * * * ? *",
					state: "1",
				},
				{
					id: "3",
					title: "清理未实名用户",
					handler: "deleteUserHandler",
					cron: "0 0 0 * * ? *",
					state: "-1",
				},
			],
		};
	},
	mounted() {
		this.$nextTick(() => {
			this.questionId = this.$route.query.id;
			console.log("question id is :" + this.questionId);
			this.detail();
		});
	},
	methods: {
		async detail() {
			var res = await this.$API.question.detail.get(this.questionId);
			console.log(res);
			var langs = res.data.languages;
			this.list = langs;
			this.question = res.data;
		},
		add() {
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog
					.open()
					.setData({ questionId: this.questionId });
			});
		},
		edit(task) {
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open("edit").setData(task);
			});
		},
		del(task) {
			this.$confirm(`确认删除 ${task.title} 计划任务吗？`, "提示", {
				type: "warning",
				confirmButtonText: "删除",
				confirmButtonClass: "el-button--danger",
			})
				.then(async () => {
					var res = await this.$API.question.languageRemove.post({
						id: task.questionLanguageId,
					});
					if (res.code == 200) {
						this.list.splice(
							this.list.findIndex(
								(item) =>
									item.questionLanguageId ===
									task.questionLanguageId
							),
							1
						);
					}
				})
				.catch(() => {
					//取消
				});
		},
		logs() {
			this.dialog.logsVisible = true;
		},
		run(task) {
			this.$message.success(`已成功执行计划任务：${task.title}`);
		},
		//本地更新数据
		handleSuccess() {
			this.detail();
			return;
		},
	},
};
</script>

<style scoped>
.task {
	height: 210px;
}
.task-item h2 {
	font-size: 15px;
	color: #3c4a54;
	padding-bottom: 15px;
}
.task-item li {
	list-style-type: none;
	margin-bottom: 10px;
}
.task-item li h4 {
	font-size: 12px;
	font-weight: normal;
	color: #999;
}
.task-item li p {
	margin-top: 5px;
}
.task-item .bottom {
	border-top: 1px solid #ebeef5;
	text-align: right;
	padding-top: 10px;
	display: flex;
	justify-content: space-between;
	align-items: center;
}

.task-add {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	text-align: center;
	cursor: pointer;
	color: #999;
}
.task-add:hover {
	color: #409eff;
}
.task-add i {
	font-size: 30px;
}
.task-add p {
	font-size: 12px;
	margin-top: 20px;
}

.dark .task-item .bottom {
	border-color: var(--el-border-color-light);
}
</style>
